package cn.deschen.biz.service.impl;

import cn.deschen.biz.domain.entity.AdCreative;
import cn.deschen.biz.domain.entity.AdUnitCreative;
import cn.deschen.biz.mapper.AdCreativeMapper;
import cn.deschen.biz.service.AdCreativeService;
import cn.deschen.biz.service.AdUnitCreativeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 创意表 服务实现类
 * </p>
 *
 * @author deschen
 * @since 2021-06-02
 */
@Service
public class AdCreativeServiceImpl extends ServiceImpl<AdCreativeMapper, AdCreative> implements AdCreativeService {

    @Autowired
    private AdUnitCreativeService unitCreativeService;

    @Override
    public void batchUnitRel(Long unitId, Collection<Long> creativeIds) {
        List<AdUnitCreative> unitCreativeList = creativeIds.stream().map(creativeId -> {
            AdUnitCreative unitCreative = new AdUnitCreative();
            unitCreative.setUnitId(unitId);
            unitCreative.setCreativeId(creativeId);
            return unitCreative;
        }).collect(Collectors.toList());
        unitCreativeService.saveBatch(unitCreativeList);
    }

    @Override
    public Set<Long> listEleIdById(Long unitId) {
        QueryWrapper<AdUnitCreative> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(AdUnitCreative::getUnitId, unitId);
        Set<Long> creativeIdSet = unitCreativeService.list(wrapper).stream().map(AdUnitCreative::getCreativeId).collect(Collectors.toSet());
        return creativeIdSet;
    }


}
